package autoteam.ui.tabs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;

import autoteam.League;
import autoteam.Child;
import autoteam.Local;
import autoteam.tables.KidsTableModel;
import autoteam.ui.AutoTeamTablePanel;
import autoteam.ui.SkillEditor;
import autoteam.ui.dialogs.PlayerCreator;
import autoteam.ui.dialogs.PlayerEditor;
import autoteam.ui.dialogs.WeightEditor;
import autoteam.ui.dialogs.WeightEditorDialog;

public class PlayersTab extends TabPanel
{
    private static final long serialVersionUID = -1785683673259571068L;
    
    private final JButton addButton = new JButton("Add");
    private final JButton removeButton = new JButton("Remove");
    private final JButton editButton = new JButton("Edit");
    private final JButton profileButton = new JButton("Profile");
    
    private JTable table; 
    
    public PlayersTab(JFrame owner)
    {
    	//DO NOT USE
    }
    
    public PlayersTab( final League _doc, JFrame owner)
    {
    	super( );
    	
    	// create the table
    	final KidsTableModel kidsModel = new KidsTableModel( _doc );
    	final AutoTeamTablePanel theTablePanel = new AutoTeamTablePanel
    	( 
    	  kidsModel,
    	  editButton,
    	  addButton
    	);
    	
    	final JButton editButton = new JButton( Local.get( "EditSkills" ) );
    	
    	editButton.addActionListener( new ActionListener( )
    	{
    	    public void actionPerformed( ActionEvent e )
    	    {
    		theTablePanel.stopEditing( );
    		int r = theTablePanel.getSelectedRow( );
    		if ( r < 0 )
    		{
    		    JOptionPane.showMessageDialog
    		    ( null, Local.get( "NoSelectionEd" ), 
    		      Local.get( "Error" ), 
    		      JOptionPane.ERROR_MESSAGE, null 
    		    );
    		    return;
    		}
    		Child c = (Child) _doc.getChildren( ).get( r );
    		if ( SkillEditor.showDialog( null, c, _doc ) )
    		    kidsModel.fireTableDataChanged( );
    		
    	    }
    	} );
    	
    	theTablePanel.addButton( editButton );
    	
    	final JButton editButton2 = new JButton( ("Edit Weights") );
    	editButton2.addActionListener( new ActionListener( )
    	{
    	    public void actionPerformed( ActionEvent e )
    	    {
    		theTablePanel.stopEditing( );
    		
    		if ( WeightEditor.showDialog( null, _doc ) )
    		    kidsModel.fireTableDataChanged( );
    		
    	    }
    	} );
    	
    	theTablePanel.addButton( editButton2 );
    	
    	this.add( theTablePanel );
        }

        @ Override
        protected void setButtonActions( final JFrame parentFrame )
        {
    	addButton.addActionListener( new ActionListener( )
    	{
    	    @ Override
    	    public void actionPerformed( ActionEvent e )
    	    {
    		PlayerCreator dialog = new PlayerCreator( parentFrame );
    		dialog.setVisible( true );
    	    }
    	} );
    	
    	editButton.addActionListener( new ActionListener( )
    	{
    	    @ Override
    	    public void actionPerformed( ActionEvent e )
    	    {
    		PlayerEditor dialog = new PlayerEditor( parentFrame );
    		dialog.setVisible( true );
    	    }
    	} );
    	
    }
    
    public PlayersTab( final League currentLeague )
    {
	super( );
	
	// create the table
	final KidsTableModel kidsModel = new KidsTableModel( currentLeague );
	final AutoTeamTablePanel theTablePanel = new AutoTeamTablePanel( kidsModel );
	
	final JButton editButton = new JButton( Local.get( "EditSkills" ) );
	
	editButton.addActionListener( new ActionListener( )
	{
	    public void actionPerformed( ActionEvent e )
	    {
		theTablePanel.stopEditing( );
		int r = theTablePanel.getSelectedRow( );
		if ( r < 0 )
		{
		    JOptionPane.showMessageDialog( null, 
		                                   Local.get( "NoSelectionEd" ), 
		                                   Local.get( "Error" ),
						   JOptionPane.ERROR_MESSAGE, null 
						 );
		    return;
		}
		Child c = (Child) currentLeague.getChildren( ).get( r );
		if ( SkillEditor.showDialog( null, c, currentLeague ) )
		    kidsModel.fireTableDataChanged( );
		
	    }
	} );
	
	theTablePanel.addButton( editButton );
	
	final JButton editButton2 = new JButton( ("Edit Weights") );
	editButton2.addActionListener( new ActionListener( )
	{
	    public void actionPerformed( ActionEvent e )
	    {
		theTablePanel.stopEditing( );
		
		if ( WeightEditorDialog.showDialog( null, currentLeague ) )
		    kidsModel.fireTableDataChanged( );
		
	    }
	} );
	
	theTablePanel.addButton( editButton2 );
	
	this.add( theTablePanel );
    }
}
